package com.sz.app.oss.api.logistics.base;

import com.sz.app.oss.constants.MessagesCodes;
import com.sz.biz.app.web.BaseController;
import com.sz.biz.logistics.core.service.ProductCustomerRelService;
import com.sz.biz.logistics.core.dto.ProductCustomerRelDto;
import com.sz.common.base.dto.ResultDto;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.Map;

/**
 * Function: ProductController <br>
 * Author: zhengpenghui <br>
 * Date: 2017-04-26 16:30:00
 */
@RestController
@RequestMapping("${api}/biz/product/customer")
@Api(description = " ", tags = "035.1、产品可见性")
public class ProductCustomerRelController extends BaseController {

    @Autowired
    private ProductCustomerRelService productCustomerRelService;

    /**
     * 添加客户产品可见性
     *
     * @throws Exception
     */
    @ApiOperation(value = "添加产品可见性", notes = "添加产品可见性")
    @RequestMapping(value = "", method = {RequestMethod.POST})
    public ResultDto saveProduct(@RequestBody @ApiParam(value = "客户可见产品", required = true) @Validated ProductCustomerRelDto productCustomerRelDto) {
        productCustomerRelService.save(productCustomerRelDto);
        return this.getSaveSuccessDto(MessagesCodes.MODULE_PRODUCT);
    }

    /**
     * 修改客户产品可见性
     *
     * @throws Exception
     */
    @ApiOperation(value = "修改客户产品可见性", notes = "修改客户产品可见性")
    @RequestMapping(value = "/update", method = {RequestMethod.POST})
    public ResultDto updateProduct(@RequestBody @ApiParam(value = "客户可见产品", required = true) @Validated ProductCustomerRelDto productCustomerRelDto) throws ParseException {
        productCustomerRelService.update(productCustomerRelDto);
        return this.getUpdateSuccessDto(MessagesCodes.MODULE_PRODUCT);
    }

    /**
     * 客户产品可见性
     */
    @ApiOperation(value = "客户产品可见性", notes = "客户产品可见性")
    @ApiImplicitParams({ @ApiImplicitParam(name = "customerId", value = "客户id", dataType = "int", paramType = "query")})
    @RequestMapping(value = "/find", method = { RequestMethod.GET })
    public ResultDto findByCustomerId(@RequestParam(required = true) Integer customerId) {
        Map<String, Object> productCustomerRelList =  productCustomerRelService.findProductAndGroupsByCustomerId(customerId);
        ResultDto dto = new ResultDto();
        dto.setData(productCustomerRelList);
        return dto;
    }
}
